package com.hjj.app;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;

public class MyApp7 {
    public static void main(String[] args) {
        // 创建结点
        Node nodeSrc = new Node("src", null);
        Node nodeMain = new Node("main", nodeSrc);
        Node nodeJava = new Node("java", nodeMain);
        Node nodeCom = new Node("com", nodeJava);
        Node nodeHJJ = new Node("hjj", nodeCom);
        Node nodeBeans = new Node("beans", nodeHJJ);
        Node nodeMapper = new Node("mapper", nodeHJJ);
        Node nodeServices = new Node("services", nodeHJJ);
        Node nodeController = new Node("controller", nodeHJJ);
        Node nodeResources = new Node("resources", nodeMain);
        Node nodeTest = new Node("test", nodeSrc);

        ArrayList<Node> nodes = new ArrayList<>();
        nodes.add(nodeSrc);
        nodes.add(nodeMain);
        nodes.add(nodeJava);
        nodes.add(nodeCom);
        nodes.add(nodeHJJ);
        nodes.add(nodeBeans);
        nodes.add(nodeMapper);
        nodes.add(nodeServices);
        nodes.add(nodeController);
        nodes.add(nodeResources);
        nodes.add(nodeTest);

        MyApp7 myApp7 = new MyApp7();


        for (Node node : nodes) {
            String path = "F:\\IO\\web";
            String execute = myApp7.execute(node);
            path += execute;

            // 获取文件对象
            File file = new File(path);
            // 创建文件夹
            boolean mkdirs = file.mkdirs();
            if (mkdirs){
                System.out.println(path + "创建成功........................");
            }else {
                System.out.println(path + "创建失败........................");
            }

        }

    }

    // 结点类型
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Node{
        private String name;
        private Node parent;
    }

    private  LinkedList<Node> linkedList = new LinkedList<>();

    // 递归父节点入栈
    protected   void getPath(Node node){
//        linkedList.push(node);
        if (node.getParent() != null){
            linkedList.push(node.getParent());
            getPath(node.getParent());
        }
    }

    // 传入目录结点，获得目录路径
    public String execute(Node node){
        // 自家入栈
        linkedList.push(node);
        // 找父节点入栈
        getPath(node);

        // 出栈或得目录路径
        StringBuffer stringBuffer = new StringBuffer();
        while (linkedList.peek() != null){
            stringBuffer.append(File.separator).append(linkedList.pop().getName());
        }

        return stringBuffer.toString();
    }
}
